package sender;

import java.util.StringTokenizer;

import common.Constants;
import common.Utilities;

public class SendArguments {
	int receiverPort = 0;
	String host = null;
	String fileName = null;
	
	public SendArguments(String[] args) {
		parseCommandLine(args);
	}
	
	public SendArguments(int recvPort, String host, String file) {
		this.receiverPort = recvPort;
		this.host = host;
		this.fileName = file;
	}
	
	private void parseCommandLine(String[] args) {		
		if(args.length < 4) {
			Utilities.printUsage(true);
			System.exit(1);
		}		
		for(int i = 0; i < args.length; i++) {
			if(args[i].equals("-r")) {
				StringTokenizer tok = new StringTokenizer(args[i + 1], ":");
				host = tok.nextToken();
				String portTmp = tok.nextToken();
				receiverPort = new Integer(portTmp);
			}
			if(args[i].equals("-f")) {
				fileName = args[i + 1];
			}
		}
		
		// validate inputs
		if(receiverPort == 0 || host == null || fileName == null) {
			Utilities.printUsage(true);
			System.exit(1);
		}
		if(receiverPort == Constants.SENDER_PORT) {
			System.out.println("Port [" + Constants.SENDER_PORT + "] is reserved for the sender. Please specify a different port for the receiver!");
			System.exit(1);
		}
	}
	
	public int getReceiverPort() {
		return receiverPort;
	}
	public String getHost() {
		return host;
	}
	public String getFileName() {
		return fileName;
	}
}
